<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
			<el-form-item label="盘点单号" prop="batchNumber">
				<el-input v-model="queryParams.batchNumber" placeholder="请输入盘点单号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="盘点主题" prop="checkTheme">
				<el-input v-model="queryParams.checkTheme" placeholder="请输入盘点主题" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="盘点日期">
				<el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
				 start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-table v-loading="loading" :data="elecMaterialCheckList">
			<el-table-column label="主键ID" align="center" prop="checkId" />
			<el-table-column label="盘点单号" align="center" prop="batchNumber" />
			<el-table-column label="盘点主题" align="center" prop="checkTheme" />
			<el-table-column label="盘点日期" align="center" prop="checkDate" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.checkDate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="备注" align="center" prop="remark" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-search" @click="handleUpdate(scope.row)" v-hasPermi="['elec:elecMaterialCheckSg:see']">查看详情</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改工器具领用对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
			<el-table v-loading="loading" :data="elecMaterialList" height="450">
				<el-table-column label="主键ID" align="center" prop="materialId" />
				<el-table-column label="工器具编码" align="center" prop="materialCode" width="100" />
				<el-table-column label="工器具名称" align="center" prop="materialName" width="200" />
				<el-table-column label="盘点结果" align="center" prop="checkResult">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.check_result" :value="scope.row.checkResult" />
					</template>
				</el-table-column>
				<el-table-column label="分类" align="center" prop="sortName" width="100" />
				<el-table-column label="库房" align="center" prop="warehouseName" width="100" />
				<el-table-column label="柜架" align="center" prop="cabinetName" width="100" />
				<el-table-column label="层位" align="center" prop="gridName" width="100" />
				<el-table-column label="供应商" align="center" prop="providerName" width="100" />
			</el-table>
			<pagination v-show="totalMaterial>0" :total="totalMaterial" :page.sync="queryMaterialParams.pageNum" :limit.sync="queryMaterialParams.pageSize"
			 @pagination="getDetailsList" />
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listElecMaterialCheck,
		getElecMaterialCheck,
		delElecMaterialCheck,
		addElecMaterialCheck,
		updateElecMaterialCheck,
		listElecMaterialCheckDetails
	} from "@/api/elec/elecMaterialCheck";

	export default {
		name: "ElecMaterialCheck",
		dicts: ['check_result'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				//日期控件
				dateRange: [],
				// 总条数
				total: 0,
				// 选择工器具列表总条数
				totalMaterial: 0,
				// 资产盘点主表格数据
				elecMaterialCheckList: [],
				// 工器具表格数据
				elecMaterialList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					batchNumber: null,
					checkTheme: null,
					checkDate: null,
					materialCategory: "2",
				},
				queryMaterialParams: {
					pageNum: 1,
					pageSize: 10,
					checkId: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					batchNumber: [{
						required: true,
						message: "盘点单号不能为空",
						trigger: "blur"
					}],
					checkTheme: [{
						required: true,
						message: "盘点主题不能为空",
						trigger: "blur"
					}],
					checkDate: [{
						required: true,
						message: "盘点日期不能为空",
						trigger: "blur"
					}],
				},
				pickerOptions: {
					shortcuts: [{
						text: '最近一周',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近一个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近三个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
							picker.$emit('pick', [start, end]);
						}
					}]
				},
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询资产盘点主列表 */
			getList() {
				this.loading = true;
				listElecMaterialCheck(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
					this.elecMaterialCheckList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			getDetailsList(){
				this.loading = true;
				this.queryMaterialParams.checkId = this.form.checkId;
				listElecMaterialCheckDetails(this.queryMaterialParams).then(response => {
					console.log(response.rows)
				  this.elecMaterialList = response.rows;
				  this.totalMaterial = response.total;
				  this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					checkId: null,
					batchNumber: null,
					checkTheme: null,
					checkDate: null,
					remark: null,
					delFlag: null,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null
				};
				this.elecMaterialList = [];
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.dateRange = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const checkId = row.checkId || this.ids
				getElecMaterialCheck(checkId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "查看盘点详情";
					
					this.getDetailsList();
				});
			},
		}
	};
</script>
